package com.dwxt.cdms.api.common.bean;

import com.alibaba.fastjson.JSON;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


/***************************************************************************
 * <PRE>
 * 
 *  Project Name    : cdms-api
 *  
 *  Description     : 血糖设备数据采集数据报文对象
 *  
 *  Author          : huke.zhang
 * 
 * </PRE>
 ***************************************************************************/
@Data
public class SrcDataValue implements Serializable {

	private static final long serialVersionUID = -1203493109929647895L;

	private String srcData;
	private String start;
	private String version;
	private String user;
	private String customer;
	private String modelCode;
	private String deviceType;
	private String serialNumber;
	private String data1;
	private String data2;
	private String data3;
	private String year;
	private String month;
	private String day;
	private String hour;
	private String minute;
	private String checkCode;
	private String separator43;
	private String imsi;
	private String separator59;
	private String latiLongitude;
	private String crcCode;


	public SrcDataValue(String srcdata) {
		this.srcData = srcdata;
		parseData();
	}

	protected void parseData() {

		this.start = this.srcData.substring(0, 2);
		this.version = this.srcData.substring(2, 4);
		this.user = this.srcData.substring(4, 5);
		this.customer = this.srcData.substring(5, 7);
		this.modelCode = this.srcData.substring(7, 9);
		this.deviceType = this.srcData.substring(9, 11); // 机种码
		this.serialNumber = this.srcData.substring(11, 20);
		this.data1 = this.srcData.substring(20, 23);
		this.data2 = this.srcData.substring(23, 26);
		// 血糖仪是以mg/dL为单位传送，如果要显示为mmol/dL的单位，则要除以18，并四舍五入才能与血糖仪端显示的数据一致
		this.data3 = this.srcData.substring(26, 29);
		this.year = this.srcData.substring(29, 31);
		this.month = this.srcData.substring(31, 33);
		this.day = this.srcData.substring(33, 35);
		this.hour = this.srcData.substring(35, 37);
		this.minute = this.srcData.substring(37, 39);
		this.checkCode = this.srcData.substring(39, 43);
		this.separator43 = this.srcData.substring(43, 44);
		this.imsi = this.srcData.substring(44, 59);
		this.separator59 = this.srcData.substring(59, 60);
		this.latiLongitude = this.srcData.substring(60, 85);

		if (this.srcData.length() > 85) {
			this.crcCode = this.srcData.substring(85);
		}
	}

	/**
	 * 检查时间
	 */
	public String getCheckTime(){
		int year = Integer.valueOf("20"+this.year);
		int month = Integer.valueOf(this.month);
		int day = Integer.valueOf(this.day);
		int hour = Integer.valueOf(this.hour);
		int minute = Integer.valueOf(this.minute);
		System.err.println(year+"_"+month+"_"+day+"_"+hour+"_"+minute);
		LocalDateTime localDateTime = LocalDateTime.of(
				year,month,day,hour,minute);
		System.err.println(JSON.toJSON(localDateTime));
		DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		String dateStr = localDateTime.format(fmt);
		return dateStr;
	}

}
